﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    WinJS.strictProcessing();

    var fill_line = new Array(420);
    var context,gametxt;

    var startX = 50; // X co-ordinate for First position of dot
    var startY = 50; //Y co-ordinate First position of dot
    var LinePs = 100; // Space between dot's
    var Margin = 10; // Margin px to draw line
    var UMargin = LinePs - Margin; // Upper Margin 
    var NP = 5  //  X * X dot's.
    var player = 'A';
    var boxcnt = [0,0,0,0,0];
    var index = 0;

    function writeMessage(canvas, message) {
        context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.font = '18pt Calibri';
        context.fillStyle = 'black';
        context.fillText(message, 10, 25);
        context.lineTo(mousePos.x, mousePos.y);
    }

    function getMousePos(canvas, evt) {
        var rect = canvas.getBoundingClientRect(), root = document.documentElement;

        // return relative mouse position
        var mouseX = evt.clientX //-  root.scrollTop -rect.top ;
        var mouseY = evt.clientY //- root.scrollLeft -rect.left;
        return {
            x: mouseX,
            y: mouseY
        };
    }

    /* check is lines active */
    function chk3(a, b, c) {
        var cnt = 0;
        if (a)
            cnt++;
        if (b)
            cnt++;
        if (c)
            cnt++;
        return (cnt);
    }
    
    /* get the co ordinates(relative position from canvas) for the index of array */
    function getxy(n)
    {
        var i,j,x,y;
        j=Math.floor(n/(2*NP-1));
        i=n%(2*NP-1);
        if(i<(NP-1))
        {
            x=startX+i*LinePs +1 ;
            y=startY+j*LinePs +1;
        }
        else
        {
            i=i-(NP-1);
            x=startX+i*LinePs;
            y=startY+j*LinePs;
        }
        return {
            x: x,
            y: y
         }
        ;
    }

    /* is box to be filles?,if so fill it */
    function isbox(n, ref, NP, context) {


        var x, y, z, box, i;
        //char *ch;
        box = 0;
        //getxy(n,&x,&y);
        if (n % (2 * NP - 1) < (NP - 1)) {  /* Horizantal line */
            /* except first row in horizantal rows,check any other box can be filled,upside */
            if ((n >= (NP - 1)) && (chk3(fill_line[n - NP], fill_line[n - NP + 1], fill_line[n - (2 * NP - 1)]) == 3)) {
                if (ref=1) {
                    // boxarry[n-(2*NP-1)]=DRASTS;
                    var result = getxy(n - (2 * NP - 1));
                    context.rect(result.x, result.y, LinePs, LinePs);
                    context.fillStyle = "red";
                    //context.fill();
                    context.fillText(player,result.x+20,result.y+20);
                    gametxt.fillText("postition 1" + result.x + "," + result.y, 10, 400);
                    box++;
                }
                
            }
            /*except last line, check any other box can be filled,downside */
            if (n < ((NP - 1) * (2 * NP - 1)) && (chk3(fill_line[n + NP], fill_line[n + NP - 1], fill_line[n + (2 * NP - 1)]) == 3)) {
                if (ref) {
                    var result = getxy(n);
                    context.rect(result.x, result.y, LinePs, LinePs);
                    context.fillStyle = "red";
                    //context.fill();
                    context.fillText(player, result.x + 20, result.y + 20);
                    gametxt.fillText("postition 2", 10, 420);
                    box++;
                }
                
            }
        }
        else { /*stright line */
            /* except first line, left side box check */
            if ((n % (2 * NP - 1)) != (NP - 1) && (chk3(fill_line[n - 1], fill_line[n - NP], fill_line[n + (NP - 1)]) == 3)) {
                if (ref) {
                    var result = getxy(n-1);
                    context.rect(result.x, result.y, LinePs, LinePs);
                    context.fillStyle = "red";
                    //context.fill();
                    context.fillText(player, result.x + 20, result.y + 20);
                    gametxt.fillText("postition 3", 10, 430);
                    box++;
                }
              }
            /* except last line,right side box check */
            if ((n % (2 * NP - 1)) != 2 * (NP - 1) && (chk3(fill_line[n + 1], fill_line[n + NP], fill_line[n - (NP - 1)]) == 3)) {
                if (ref) {
                    var result = getxy(n);
                    context.rect(result.x, result.y, LinePs, LinePs);
                    context.fillStyle = "red";
                    //context.fill();
                    context.fillText(player, result.x + 20, result.y + 20);
                    gametxt.fillText("postition 4", 10, 430);
                    box++;
                }
              
            }
        }
        return box;
    }

    window.onload = function () {
        var canvas = document.getElementById('myCanvas');
         context = canvas.getContext('2d');
       var canvas1 = document.getElementById('game1');
       gametxt = canvas1.getContext('2d');
        
        for (var i = 0; i < 420; i++) {
            fill_line[i] = 0;
        }

        for (var i = 0; i < NP; i++) {
            for (var j = 0; j < NP; j++) {
                context.fillRect(startX + j * LinePs, startY + i * LinePs, 3, 3);
            }
        }

        canvas.addEventListener('click', function (evt) {

            var rect = canvas.getBoundingClientRect(), root = document.documentElement;

            // return relative mouse position
            var mouseX = evt.clientX - root.scrollTop - rect.left;
            var mouseY = evt.clientY - root.scrollLeft - rect.top;

            var message = "Mouse position: " + mouseX + "," + mouseY;//+ "," + rect.top + "," + rect.left;

            context = canvas.getContext('2d');
            //context.save();
            //context.setTransform(1, 0, 0, 1, 0, 0);
            //context.clearRect(0, 0, canvas.width, canvas.height);

            //context.restore();
            context.font = '18pt Calibri';
            context.fillStyle = 'black';
            var x, y, n;

            for (var i = 0; i < NP; i++) {
                //fill_line[i]= new Array(NP);
                for (var j = 0; j < NP; j++) {
                    context.fillRect(startX + j * LinePs, startY + i * LinePs, 3, 3);
                }
            }
            gametxt.font = '18pt Calibri';
            gametxt.fillStyle = 'black';
            gametxt.clearRect(0, 0, canvas1.width, canvas1.height);
            gametxt.fillText(message, 10, 25);
            var rmousex = mouseX - startX;
            var rmousey = mouseY - startY;
            /* is it valid mouse postion to draw line? */
            if (mouseX > startX - Margin && mouseY > startX - Margin && mouseX < startX + Margin + NP * LinePs && mouseY < startY + Margin + NP * LinePs) {

                var l = (rmousex % LinePs > UMargin) ? (rmousex + LinePs - rmousex % LinePs) : (rmousex - rmousex % LinePs);
                var p = (rmousey % LinePs > UMargin) ? (rmousey + LinePs - rmousey % LinePs) : (rmousey - rmousey % LinePs);
                /* horizantal line */
                if ((rmousey % LinePs < Margin || rmousey % LinePs > UMargin) && (rmousex % LinePs > Margin && rmousex % LinePs < UMargin)) {
                    mouseX = rmousex - (rmousex % LinePs);
                    x = (mouseX) / LinePs;
                    y = (p) / LinePs;
                    n = x + (y * (2 * NP - 1));
                    if (fill_line[n] == 0) {
                        context.moveTo(mouseX + startX + 1, p + startY + 1);
                        context.lineTo.apply(context, [mouseX + startX + LinePs + 1, p + startY + 1]);
                        fill_line[n] = 1;
                        
                        var countbox = isbox(n, 1, NP, context);
                        
                        boxcnt[index] = boxcnt[index] + countbox;
                        if (countbox == 0) {

                            player = (player == 'A') ? 'B' : 'A';
                            index = (index == 0) ? 1 : 0;
                        }

                    }
                }
                    /* vertical line */
                else if ((rmousex % LinePs < Margin || rmousex % LinePs > UMargin) && (rmousey % LinePs > Margin && rmousey % LinePs < UMargin)) {
                    mouseY = rmousey - (rmousey % LinePs);

                    x = l / LinePs;
                    y = mouseY / LinePs;
                    n = x + (y * (2 * NP - 1)) + NP - 1;
                    if (fill_line[n] == 0) {
                        context.moveTo(l + startX + 1, mouseY + startY + 1);
                        context.lineTo.apply(context, [l + startX + 1, mouseY + startY + LinePs + 1]);
                        fill_line[n] = 1;
                        var countbox = isbox(n, 1, NP, context);

                        boxcnt[index] = boxcnt[index] + countbox
                        if (countbox == 0) {
                            player = (player == 'A') ? 'B' : 'A';
                            index = (index == 0) ? 1 : 0;
                        }
                    }
                }
            }
            context.stroke();
            gametxt.fillText("Array index," + n + ", " + evt.clientX + "," + evt.clientY + "   " + player, 10, 550);
            gametxt.fillText("Player A score:"+boxcnt[0],10,450);
            gametxt.fillText("Player B score:"+boxcnt[1],10,350);
        },false);
    };

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.start();
})();
